/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.nodes.sprites;

import com.android1.amarena2d.commons.BlendFunction;
import com.android1.amarena2d.input.EntityTouchSpot;
import com.android1.amarena2d.nodes.BaseLeafEntity;
import com.android1.amarena2d.nodes.NodeEntity;
import com.android1.amarena2d.nodes.behavior.CollidableEntity;
import com.android1.amarena2d.nodes.behavior.Touchable;
import com.android1.amarena2d.nodes.behavior.delegates.EntityCollisionDelegate;
import com.android1.amarena2d.nodes.behavior.delegates.EntityTouchDelegate;
import com.android1.amarena2d.nodes.behavior.delegates.TouchDelegate;
import com.android1.amarena2d.texture.TextureFrame;

/**
 * Provides base Sprite functionality except rendering.
 */
public abstract class BaseSprite<T extends NodeEntity> extends BaseLeafEntity<T> implements CollidableEntity, Touchable<EntityTouchSpot> {

    protected EntityCollisionDelegate collisionDelegate;


    protected BlendFunction blendFunction = BlendFunction.DEFAULT;

    public BaseSprite(float x, float y) {
        this.position.x = x;
        this.position.y = y;
    }

    @Override
    public EntityCollisionDelegate collision() {
        if (collisionDelegate == null)
            collisionDelegate = new EntityCollisionDelegate(this);
        return collisionDelegate;
    }

    @Override
    @SuppressWarnings("unchecked")
    public TouchDelegate<EntityTouchSpot> touch() {
        if (touchDelegate == null)
            touchDelegate = new EntityTouchDelegate(this);
        return touchDelegate;
    }


    public BlendFunction getBlendFunction() {
        return blendFunction;
    }

    public void setBlendFunction(BlendFunction blendFunction) {
        this.blendFunction = blendFunction;
    }

    public abstract TextureFrame getTextureFrame();

    public abstract void setTextureFrame(TextureFrame textureFrame);

    @Override
    public void enter() {
        if (collisionDelegate != null)
            collisionDelegate.register();
        super.enter();
    }

    @Override
    public void exit() {
        if (collisionDelegate != null)
            collisionDelegate.unregister();
        super.exit();
    }


}
